<?php
	class Order_model extends CI_Model {	        

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    public function getOrder($id)
    {
        $this->db->select('*');
        $this->db->from('orders');
        $this->db->where('id',$id);

        $query = $this->db->get();

        return $query->row();
    }

    public function insert($data)
    {
        $this->db->insert('orders',$data);
    }

    public function update($data, $id)
    {
        $this->db->where('id', $id);
        $this->db->update('orders',$data);
    }

    // paging load
    function loadAllV3($start = false, $limit = false, $search = false)
    {
        $this->db->select('orders.*');
        $this->db->from('orders');
        $this->db->join('products','products.id = orders.product_id');        

        if(!empty($search))
        {
            if(!empty($search['customer_name']))
            {
            	$this->db->like('orders.customer_name', $search['customer_name']);
            }
            if(!empty($search['customer_phone']))
            {
            	$this->db->where('orders.customer_phone', $search['customer_phone']);
            }
            if(!empty($search['order_status']))
            {
                $this->db->where('orders.status', $search['order_status']);
            }
            if(!empty($search['from_date']) && !empty($search['to_date']))
            {
                $search['from_date'] = date('Y-m-d', strtotime($search['from_date']));
                $search['to_date'] = date('Y-m-d', strtotime($search['to_date']));

            	$this->db->where('orders.created_at >', $search['from_date']);
            	$this->db->where('orders.created_at <', $search['to_date']);	
            }
            // granted to user
            if(!empty($search['user_id']))
            {
                $this->db->where('products.user_id', $search['user_id']);
            }
        }

        $this->db->order_by('orders.id','desc');
        $this->db->limit($limit,$start);

        $query = $this->db->get();                      

        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->countResultV2($start,$limit,$search),
            );
        
        return $return;
    }

    function countResultV2($start = false, $limit = false,$search = false)
    {
        $this->db->select('orders.*');
        $this->db->from('orders');
        $this->db->join('products','products.id = orders.product_id');        

        if(!empty($search))
        {
            if(!empty($search['customer_name']))
            {
            	$this->db->like('orders.customer_name', $search['customer_name']);
            }
            if(!empty($search['customer_phone']))
            {
            	$this->db->where('orders.customer_phone', $search['customer_phone']);
            }
            if(!empty($search['from_date']) && !empty($search['to_date']))
            {
            	$this->db->where('orders.created_at', $search['from_date']);
            	$this->db->where('orders.created_at', $search['to_date']);	
            }
        }
        return $this->db->count_all_results();
    }
}    